package client;

import server.ServerConstants;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

/**
 * Created by IntelliJ IDEA.
 * User: Hp
 * Date: 25.05.11
 * Time: 1:22
 * To change this template use File | Settings | File Templates.
 */
public class ClientApplet extends JApplet {

    private JComboBox figure, colors;
    private PaintingModel model;
    private PaintingView view;
    private GridBagConstraints gbc;
    private ClientTCP clientTCP;
    private JButton clear;
    private Color c;
    private JPanel container;
    private MyPreviewPanel pre;
    private JToolTip tool;
    private JPanel p;

    @Override
    public void init() {
        container = new JPanel();
        pre = new MyPreviewPanel();
        gbc = new GridBagConstraints();
        view = new PaintingView();
        model = new PaintingModel("Masha", 300, 300);
        figure = new JComboBox();
        clear = new JButton("Clear");
        String[] figureNames = {"Circle", "Rectangle", "Line", "Point"};
        for (String f : figureNames)
            figure.addItem(f);
        figure.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (view != null) {
                    PaintAction obj;
                    switch (figure.getSelectedIndex()) {
                        case 0:
                            obj = new PaintActionCircle();
                            obj.setColor(c);
                            view.setActionType(obj);
                            break;
                        case 1:
                            obj = new PaintActionRectangle();
                            obj.setColor(c);
                            view.setActionType(obj);
                            break;
                        case 2:
                            obj = new PaintActionLine();
                            obj.setColor(c);
                            view.setActionType(obj);
                            break;
                        case 3:
                            obj = new PaintAction();
                            obj.setColor(c);
                            view.setActionType(obj);
                            break;
                    }
                }
            }
        });
        figure.setSelectedIndex(3);
        colors = new JComboBox();
        String[] colorNames = {"Black", "White", "Red", "Green", "Blue"};
        for (String s : colorNames)
            colors.addItem(s);
        colors.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                switch (colors.getSelectedIndex()) {
                    case 0:
                        c = Color.black;

                        break;
                    case 1:
                        c = Color.white;
                        break;
                    case 2:
                        c = Color.red;
                        break;
                    case 3:
                        c = Color.green;
                        break;
                    case 4:
                        c = Color.blue;
                        break;
                }
                PaintAction obj = view.getActionType();
                obj.setColor(c);
                view.setActionType(obj);
                pre.update();
                pre.repaint();
            }
        });
        view.setModel(model);
        view.setPreferredSize(new Dimension(300, 300));
        clear.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                view.setActionType(new PaintActionClearRectangle());

            }
        });
        Lamp l = new Lamp();
    }

    class Lamp extends JComponent {
        private Timer t = new Timer(1000, new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                changeStatus();
            }
        });

        Lamp() {
            super();
            setPreferredSize(new Dimension(20, 20));
            setEnabled(false);
            t.start();
        }

        void changeStatus() {
            repaint();
        }

        public void paint(Graphics g) {
            g.setColor(clientTCP.isConnected() ? Color.green : Color.red);
            tool.setToolTipText(clientTCP.isConnected() ? "online" : "offline");
            g.fillRect(0, 0, getWidth() - 1, getHeight() - 1);
        }


    }

    @Override
    public String getParameter(String name) {
        return super.getParameter(name);    //To change body of overridden methods use File | Settings | File Templates.
    }

    public void start() {
        try {
            ServerConstants.port = Integer.parseInt(getParameter("port"));
        } catch (NumberFormatException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        ServerConstants.serverHost = getCodeBase().getHost();
        clientTCP = new ClientTCP();
        model.setClientInterface(clientTCP);
        clientTCP.connect();

        p = new JPanel();
        p.setPreferredSize(new Dimension(101,400));
        gbc.gridy = GridBagConstraints.RELATIVE;
        gbc.gridx = 0;
        p.setLayout(new GridBagLayout());

        gbc.gridwidth = 1;
        gbc.gridheight = 1;
        gbc.fill = GridBagConstraints.BOTH;
        Lamp l = new Lamp();
        JLabel status=new JLabel("Status:");
        p.add(status);
        p.add(l, gbc);
        p.add(figure, gbc);
        container.add(view, gbc);

        gbc.gridx = 0;
        tool = new JToolTip();
        tool.setComponent(l);
        tool.setToolTipText("Connection");
        gbc.gridheight = 1;
        p.add(clear, gbc);
        gbc.gridx=0;
        gbc.gridwidth = 3;
        p.add(colors,gbc);
        p.add(pre, gbc);
        getContentPane().setLayout(new BorderLayout());
        getContentPane().add(container, BorderLayout.EAST);
        getContentPane().add(p, BorderLayout.WEST);
        clientTCP.setActive(true);
    }

    public void stop() {
        clientTCP.setActive(false);
    }

    class MyPreviewPanel extends JComponent {
        Color curColor;

        public MyPreviewPanel() {
            curColor = c;
            setPreferredSize(new Dimension(20, 20));
        }
        public void update(){
            curColor=c;
        }

        public void paint(Graphics g) {
            g.setColor(curColor);
            g.fillRect(0, 0, getWidth() - 1, getHeight() - 1);
        }
    }

}
